﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Publishing.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace SlightlyEnhancedContentByQueryWebPartSolution
{
    public class SlightlyEnhancedCQWPEditorPart : ToolPart
    {
        private AssetUrlSelector tbMainXslLink;
        private AssetUrlSelector tbHeaderXslLink;
        private AssetUrlSelector tbItemXslLink;
        private TextBox tbQueryOverride;
        private TextBox tbCommonViewFields;
        private CheckBox cbSuppressWebPartChrome;

        public SlightlyEnhancedCQWPEditorPart()
        {
            this.ID = "SlightlyEnhancedCQWPEditorPart";            
        }

        protected override void OnInit(EventArgs e)
        {
            SyncChanges();
            base.OnInit(e);
        }

        private TextBox CreateTextBox()
        {
            TextBox tb = new TextBox();
            tb.Rows = 4;
            tb.TextMode = TextBoxMode.MultiLine;
            tb.Width = Unit.Percentage(100);
            return tb;
        }

        private AssetUrlSelector CreateSelector()
        {
            AssetUrlSelector selector = new AssetUrlSelector();
            selector.AllowExternalUrls = false;
            selector.AssetPickerButtonVisible = true;
            selector.AssetUrlTextBoxVisible = true;
            selector.ButtonType = ButtonType.Button;
            selector.PickerButtonText = "...";
            selector.UseImageAssetPicker = false;
            selector.ValidateUrl = true;
            return selector;
        }

        protected override void CreateChildControls()
        {
            this.Title = "SE CQWP Tool Part";

            #region Create Xsl controls
            tbMainXslLink = CreateSelector();
            tbHeaderXslLink = CreateSelector();
            tbItemXslLink = CreateSelector();
            #endregion

            #region Create CQWP controls
            tbCommonViewFields = CreateTextBox();
            tbQueryOverride = CreateTextBox();
            cbSuppressWebPartChrome = new CheckBox();
            #endregion

            Table sectionTableXsl;
            Control sectionXsl = EditorPartHelper.createConfigureSectionControls(this , "Expand Category: XSLTLinks", "Collapse Category: XSLT" , "XSLT" , "XSLT", "" , out sectionTableXsl) ;
            EditorPartHelper.addToolPaneControl(sectionTableXsl, "MainXslLink", tbMainXslLink);
            EditorPartHelper.addToolPaneControl(sectionTableXsl, "HeaderXslLink", tbHeaderXslLink);
            EditorPartHelper.addToolPaneControl(sectionTableXsl, "ItemXslLink", tbItemXslLink);
            Controls.Add(sectionXsl);
            
            Table sectionTableCQWP;
            Control sectionCQWP = EditorPartHelper.createConfigureSectionControls(this, "Expand Category: CQWP", "Collapse Category: CQWP", "CQWP", "CQWP", "", out sectionTableCQWP);
            EditorPartHelper.addToolPaneControl(sectionTableCQWP, "CommonViewFields", tbCommonViewFields);
            EditorPartHelper.addToolPaneControl(sectionTableCQWP, "QueryOverride", tbQueryOverride);
            EditorPartHelper.addToolPaneControl(sectionTableCQWP, "SuppressWebPartChrome", cbSuppressWebPartChrome);
            Controls.Add(sectionCQWP);
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            ContentByQueryWebPart webpart = this.WebPartToEdit as ContentByQueryWebPart;
            if (webpart != null)
            {
                tbMainXslLink.AssetUrl = webpart.MainXslLink;
                tbHeaderXslLink.AssetUrl = webpart.HeaderXslLink;
                tbItemXslLink.AssetUrl = webpart.ItemXslLink;
                tbQueryOverride.Text = webpart.QueryOverride;
                tbCommonViewFields.Text = webpart.CommonViewFields;
                cbSuppressWebPartChrome.Checked = webpart.SuppressWebPartChrome ;
            }
        }

        public override void ApplyChanges()
        {            
            ContentByQueryWebPart webpart = this.WebPartToEdit as ContentByQueryWebPart;
            if (webpart != null)
            {
                EnsureChildControls();
                webpart.ItemXslLink = tbItemXslLink.AssetUrl;
                webpart.HeaderXslLink = tbHeaderXslLink.AssetUrl;
                webpart.MainXslLink = tbMainXslLink.AssetUrl;
                webpart.CommonViewFields = tbCommonViewFields.Text;
                webpart.QueryOverride = tbQueryOverride.Text;
                webpart.SuppressWebPartChrome = cbSuppressWebPartChrome.Checked;
            }
        }
    }
}
